package com.example.app_down_test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.histone.lib_download.downloader.FileInfo;
import com.histone.lib_download.localbroadcast.LocalBroadcastHolder;


public class LocalReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || TextUtils.isEmpty(intent.getAction())) return;
        if (intent.getAction().equals(LocalBroadcastHolder.LOCAL_BROADCAST_ACTION)) {
            int code = intent.getIntExtra(LocalBroadcastHolder.BROADCAST_CODE, 0);
            if (code == LocalBroadcastHolder.CODE_OK) {
                //成功下载
                FileInfo serializableExtra = (FileInfo) intent.getSerializableExtra(LocalBroadcastHolder.BROADCAST_FILE_INFO);
                assert serializableExtra != null;
                Log.e("file-broadcast-success", serializableExtra.toString());
                Toast.makeText(context, serializableExtra.toString(), Toast.LENGTH_SHORT).show();
            } else if (code == LocalBroadcastHolder.CODE_ERROR) {
                //下载失败的
//                FileInfo serializableExtra = (FileInfo) intent.getSerializableExtra(LocalBroadcastHolder.BROADCAST_FILE_INFO);
//                assert serializableExtra != null;
                Log.e("file-broadcast-error"," serializableExtra.toString()");
            } else if (code == LocalBroadcastHolder.CODE_ERROR_NO_SPACE) {
                Toast.makeText(context, "空间不足！", Toast.LENGTH_SHORT).show();
            }

        }
    }
}
